﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace NJL.WebManage.Company
{
    public partial class EditCompany : BasePage
    {
        NJL.BLL.NJL_COMPANY bll = new BLL.NJL_COMPANY();
        protected NJL.Model.NJL_COMPANY model;
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        NJL.BLL.NJL_CUSTOM_AREA bllCustom = new BLL.NJL_CUSTOM_AREA();
        protected void Page_Load(object sender, EventArgs e)
        {
            model = bll.GetModel(Request.QueryString["id"].ToString());
            if (!IsPostBack)
            {
                DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
                ddlProvince.DataSource = ds;
                ddlProvince.DataTextField = "AREA_NAME";
                ddlProvince.DataValueField = "AREA_CODE";
                ddlProvince.DataBind();
                if (WEBSITE_CODE == "1")
                {

                }
                else
                {
                    ddlProvince.Visible = false;

                }
                string ProvinceId = model.AREA_CODE.Substring(0, 2);
                DataSet dscity1 = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                ddlCity.DataSource = dscity1;
                ddlCity.DataTextField = "AREA_NAME";
                ddlCity.DataValueField = "AREA_CODE";
                ddlCity.DataBind();
                litTitle.Text = Request.QueryString["typeName"].ToString();
                txtName.Text = model.COMPANY_NAME;
                txtCompanyNameSms.Text = model.COMPANY_NAME_SMS;
                txtAddress.Text = model.ADDRESS;
                txtLinkman.Text = model.CONTACT;
                txtTel.Text = model.TEL;
                txtPhone.Text = model.MOBILE;
                txtQQ.Text = model.QQ;
                txtMsn.Text = model.MSN;
                txtServiceProject.Text = model.COMPANY_DESCRIPTION;
                txtTrait.Text = model.BEST_SIGN;
                txtTitle.Text = model.SEO_TITLE;
                txtKeyWords.Text = model.SEO_KEYWORDS;
                txtDescription.Text = model.SEO_DESCRIPTION;
                txtMapAddress.Text = model.MAP_ADDRESS;

                txtCompanyIntro.Text = model.COMPANY_INTRO;
                txtJTZN.Text = model.TRAFFIC_GUIDE;
                txtSort.Text = model.SORT.ToString();

                txtTICKET_NOTICE.Text = model.TICKET_NOTICE;

                txtPostCode.Text = model.POST_CODE;
                txtFax.Text = model.FAX;
                txtBindUser.Text = model.NJL_USER_ID;

                if (model.AUDIT_STATE_1 == 1)
                {
                    ckbAudit1.Checked = true;
                }
                if (model.AUDIT_STATE_2 == 1)
                {
                    ckbAudit2.Checked = true;
                }
                if (model.AUDIT_STATE_3 == 1)
                {
                    ckbAudit3.Checked = true;
                }
                if (model.AREA_CODE != "" || model.AREA_CODE != null)
                {
                    ddlCity.Items.FindByValue(model.AREA_CODE).Selected = true;
                    string province = model.AREA_CODE.Substring(0, 2);
                    DataSet dsProvince = bllArea.GetList("AREA_CODE like '" + province + "0000'");
                    ddlProvince.Items.FindByValue(dsProvince.Tables[0].Rows[0]["AREA_CODE"].ToString()).Selected = true;
                }
                if (!string.IsNullOrEmpty(model.IMAGE_CODE))
                {
                    Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                }
                else
                {
                    Image1.Visible = false;
                }
                if (!string.IsNullOrEmpty(model.AUDIT_INFO_1))
                {
                    Image2.ImageUrl = GetImageUrl(model.AUDIT_INFO_1);
                    Image2.Attributes.Add("onlick", Image2.ImageUrl);
                }
                else
                {
                    Image2.Visible = false;
                }
                if (!string.IsNullOrEmpty(model.AUDIT_INFO_2))
                {
                    Image3.ImageUrl = GetImageUrl(model.AUDIT_INFO_2);
                }
                else
                {
                    Image3.Visible = false;
                }
                if (!string.IsNullOrEmpty(model.AUDIT_INFO_3))
                {
                    Image4.ImageUrl = GetImageUrl(model.AUDIT_INFO_3);
                }
                else
                {
                    Image4.Visible = false;
                }
                rblIsPay.SelectedValue = model.IS_OPEN_AGENT_FEE.ToString();
                rblContact.SelectedValue = model.IS_SHOW_CONTACT.ToString();
                rblyd.SelectedValue = model.IS_OPEN_BOOK.ToString();
                rblCompanyOrHolidayVilla.SelectedValue = model.TYPE_CODE;
                txtSMS.Text = model.RECEIVE_SMS_MOBILE;
                ProvinceId = ddlCity.SelectedValue;
                ds = bllCustom.GetList("CITY_CODE='" + ProvinceId + "'");
                ddlArea.DataSource = ds;
                ddlArea.DataTextField = "CUSTOM_AREA_NAME";
                ddlArea.DataValueField = "ID";
                ddlArea.DataBind();
                ListItem list = new ListItem("------", "");
                ddlArea.Items.Insert(0, list);
                ddlArea.SelectedValue = model.CUSTOM_AREA_ID;
                txtWebSiteId.Text = model.COMPANY_NO;
                txtWebSitePwd.Text = model.USER_PASSWORD;
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            model.AREA_CODE = ddlCity.SelectedValue;
            model.COMPANY_NAME = txtName.Text;
            model.COMPANY_NAME_SMS = txtCompanyNameSms.Text.Trim();
            model.ADDRESS = txtAddress.Text;
            model.CONTACT = txtLinkman.Text;
            model.TEL = txtTel.Text;
            model.MOBILE = txtPhone.Text;
            model.QQ = txtQQ.Text;
            model.MSN = txtMsn.Text;
            model.UPDATE_DATE_TIME = DateTime.Now;
            model.COMPANY_DESCRIPTION = txtServiceProject.Text;
            model.BEST_SIGN = txtTrait.Text;
            model.SEO_TITLE = txtTitle.Text;
            model.SEO_KEYWORDS = txtKeyWords.Text;
            model.SEO_DESCRIPTION = txtDescription.Text;
            model.UPDATE_USER_ID = USER_ID;
            model.UPDATE_USER_NAME = USER_NAME;
            model.SORT = int.Parse(txtSort.Text.Trim());
            model.MAP_ADDRESS = txtMapAddress.Text.Trim();
            model.COMPANY_INTRO = txtCompanyIntro.Text.Trim();
            model.TRAFFIC_GUIDE = txtJTZN.Text.Trim();
            model.IS_SHOW_CONTACT = int.Parse(rblContact.SelectedValue);
            model.IS_OPEN_BOOK = int.Parse(rblyd.SelectedValue);
            model.RECEIVE_SMS_MOBILE = txtSMS.Text.Trim();
            model.TYPE_CODE = rblCompanyOrHolidayVilla.SelectedValue;
            model.TICKET_NOTICE = txtTICKET_NOTICE.Text;

            model.POST_CODE = txtPostCode.Text.Trim();
            model.FAX = txtFax.Text.Trim();

            if (ckbAudit1.Checked)
            {
                model.AUDIT_STATE_1 = 1;
            }
            else
            {
                model.AUDIT_STATE_1 = 0;
            }
            if (ckbAudit2.Checked)
            {
                model.AUDIT_STATE_2 = 1;
            }
            else
            {
                model.AUDIT_STATE_2 = 0;
            }
            if (ckbAudit3.Checked)
            {
                model.AUDIT_STATE_3 = 1;
            }
            else
            {
                model.AUDIT_STATE_3 = 0;
            }
            if (txtBindUser.Text.Trim() != "")
            {
                model.NJL_USER_ID = txtBindUser.Text.Trim();
            }
            model.IS_OPEN_AGENT_FEE = Convert.ToInt32(rblIsPay.SelectedValue);
            model.CUSTOM_AREA_ID = ddlArea.SelectedValue;
            model.USER_PASSWORD = txtWebSitePwd.Text.Trim();
            DataSet ds = bll.GetAllList("COMPANY_NO is not null and COMPANY_NO!='' and ID!=" + model.ID);
            foreach (DataRow listmodel in ds.Tables[0].Rows)
            {
                if (listmodel["COMPANY_NO"].Equals(txtWebSiteId.Text.Trim()))
                {
                    Royalsoft.Common.MessageBox.Show(this, "网编编号重复请使用其他编号");
                    return;
                }
            }
            model.COMPANY_NO = txtWebSiteId.Text.Trim();
            if (bll.Update(model))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "EditCompany.aspx?id=" + Request.QueryString["id"] + "&typeid=" + Request.QueryString["typeid"] + "&typeName=" + Request.QueryString["typeName"] + "&QQ=" + Request.QueryString["QQ"]);
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProvinceId = ddlProvince.SelectedValue.Substring(0, 2);
            DataSet ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "AREA_NAME";
            ddlCity.DataValueField = "AREA_CODE";
            ddlCity.DataBind();
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                        if (model.IMAGE_CODE != "")
                        {
                            Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                            Image1.Visible = true;
                        }
                        else
                        {
                            Image1.Visible = false;
                        }
                    }
                }
            }

        }

        protected void btnUpload2_Click(object sender, EventArgs e)
        {
            if (FileUpload2.HasFile)
            {
                model.AUDIT_INFO_1 = base.SaveUploadFile(Request.Files, 1);
                if (bll.Update(model))
                {
                    Image2.ImageUrl = GetImageUrl(model.AUDIT_INFO_1);
                    Image2.Visible = true;
                }
            }
        }

        protected void btnUpload3_Click(object sender, EventArgs e)
        {
            if (FileUpload3.HasFile)
            {
                model.AUDIT_INFO_2 = base.SaveUploadFile(Request.Files, 2);
                if (bll.Update(model))
                {
                    Image3.ImageUrl = GetImageUrl(model.AUDIT_INFO_2);
                    Image3.Visible = true;
                }
            }
        }

        protected void btnUpload4_Click(object sender, EventArgs e)
        {
            if (FileUpload4.HasFile)
            {
                model.AUDIT_INFO_3 = base.SaveUploadFile(Request.Files, 3);
                if (bll.Update(model))
                {
                    Image4.ImageUrl = GetImageUrl(model.AUDIT_INFO_3);
                    Image4.Visible = true;
                }
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProvinceId = ddlCity.SelectedValue;
            DataSet ds = bllCustom.GetList("CITY_CODE='" + ProvinceId + "'");
            ddlArea.DataSource = ds;
            ddlArea.DataTextField = "CUSTOM_AREA_NAME";
            ddlArea.DataValueField = "ID";
            ddlArea.DataBind();
            ListItem list = new ListItem("------", "000000");
            ddlArea.Items.Insert(0, list);
        }



    }
}